package m.html.list;

import m.html.element.Button;
import m.html.element.Element;
import m.html.element.Text;
import m.html.element.attr.ButtonType;
import m.html.element.attr.Size;
import m.html.element.other.Param;
import m.system.util.StringUtil;

import java.util.Set;

public class ListButton extends Button implements IListElement {
	public static ListButton modal(String title,ButtonType type,String url,int openWidth) {
		return new ListButton(title,type,new ListEvent(url,EventType.MODAL,openWidth));
	}
	public static ListButton modal(String title,ButtonType type,String url,int openWidth,SuccessMethod success) {
		return new ListButton(title,type,new ListEvent(url,EventType.MODAL,openWidth,success));
	}
	public static ListButton drawer(String title,ButtonType type,String url,int openWidth) {
		return new ListButton(title,type,new ListEvent(url,EventType.DRAWER,openWidth));
	}
	public static ListButton drawer(String title,ButtonType type,String url,int openWidth,SuccessMethod success) {
		return new ListButton(title,type,new ListEvent(url,EventType.DRAWER,openWidth,success));
	}
	public static ListButton popover(String title,ButtonType type,String url,int openWidth) {
		return new ListButton(title,type,new ListEvent(url,EventType.POPOVER,openWidth));
	}
	public static ListButton popover(String title,ButtonType type,String url,int openWidth,SuccessMethod success) {
		return new ListButton(title,type,new ListEvent(url,EventType.POPOVER,openWidth,success));
	}
	public static ListButton ajax(String title,ButtonType type,String url) {
		return new ListButton(title,type,new ListEvent(url,EventType.AJAX));
	}
	public static ListButton ajax(String title,ButtonType type,String url,SuccessMethod success) {
		return new ListButton(title,type,new ListEvent(url,EventType.AJAX,10,success));
	}
	public static ListButton page(String title,ButtonType type,String url) {
		return new ListButton(title,type,new ListEvent(url,EventType.PAGE));
	}
	public static ListButton page(String title,ButtonType type,String url,SuccessMethod success) {
		return new ListButton(title,type,new ListEvent(url,EventType.PAGE,10,success));
	}
	public static ListButton open(String title,ButtonType type,String url){
		return new ListButton(title,type,new ListEvent(url,EventType.OPEN));
	}
	public static ListButton window(String title,ButtonType type,String url){
		return new ListButton(title,type,new ListEvent(url,EventType.WINDOW));
	}
	public static ListButton back(String title,ButtonType type){
		return new ListButton(title,type,new ListEvent("",EventType.BACK));
	}
	public static ListButton notify(String title,ButtonType type){
		return new ListButton(title,type,new ListEvent("",EventType.NOTIFY));
	}

	/**
	 * 导入Excel按钮
	 * 可增加参数importParam
	 */
	public static ListButton importExcel(String title,ButtonType type,String action){
		return modal(title,type,"page/base/util/importExcel.js",700,SuccessMethod.REFRESH)
			.addQueryParam(Param.value("title",title))
			.addQueryParam(Param.value("action",action));
	}
	private final ListEvent event;//事件
	private String confirm;
	private String test;
	
	private ListButton(String title,ListEvent event) {
		this(title, ButtonType.DEFAULT,event);
	}
	private ListButton(String title,ButtonType type,ListEvent event) {
		super(Size.SMALL,type);
		this.event=event;
		super.E(new Text(title));
	}
	public ListEvent getEvent() {
		return event;
	}
	public ListButton setConfirm(String confirm) {
		this.confirm=confirm;
		return this;
	}
	public String getConfirm() {
		return this.confirm;
	}

	public String getTest() {
		return test;
	}

	/**
	 * 设置if表达式 {text, record, index} 当前列字段，行记录，行
	 * 	 *  test 表达式   例如:text=='1'
	 */
	public ListButton setTest(String test) {
		this.test = test;
		return this;
	}

	/**
	 * 添加事件参数
	 *  p
	 * 
	 */
	public ListButton addParam(Param p) {
		this.event.addParam(p);
		return this;
	}
	public ListButton addParam(String name,String field) {
		this.addParam(Param.field(name, field));
		return this;
	}
	/**
	 * 添加参数  构造VueCollectResult时才能使用
	 */
	public ListButton addParam(String a,Param p) {
		this.event.addParam(a,p);
		return this;
	}
	/**
	 * 添加事件查询参数
	 *  p
	 * 
	 */
	public ListButton addQueryParam(Param p) {
		this.event.addQueryParam(p);
		return this;
	}
	public ListButton addQueryParam(String name,String field) {
		this.addQueryParam(Param.field(name, field));
		return this;
	}
	/**
	 * 获取参数所需的字段
	 * 
	 */
	public Set<String> getParamFields() {
		return this.getEvent().getParamFields();
	}
	@Override
	public String toString() {
		Element e=event.toEventElement(new Button(this.getSize(),this.getType()).E(this.getChilds()),this.confirm);
		if(!StringUtil.isSpace(this.test)) e.A("v-if",this.test);
		return e.toString();
	}
	
}
